#################################################################################################
#################################################################################################
## Code for replicating analyses in "Buying Brokers: Electoral Handouts beyond Clientelism in a Weak-party State" 
#################################################################################################

# Clear Working Directory
rm(list=ls())

# Load Libraries
library(reshape)
library(foreign)
library(gtools)
library(stargazer)
library(xtable)
library(scales)
library(plotrix)
library(Rmisc)
library(list)
library(MASS)

setwd([insert path here])

#################################################################################################
# Table 1 lists expectations -- no code is required
#################################################################################################

#################################################################################################
# Broker Survey Data for Tables 2-5
#################################################################################################
setwd([insert path here])

# Clean the broker survey data
timses <- read.spss("Timses (332)-oke.sav", to.data.frame=TRUE, use.value.labels=FALSE, trim_values=TRUE)
timses[503,"no_kode_responden"] <- 931
timses$no_kuesioner[timses$no_kuesioner=="   "] <- NA
timses <- timses[!is.na(timses$no_kuesioner),]

timses <- timses[order(timses$no_kode_responden),]
timses$no_kode_responden <- as.numeric(as.character(timses$no_kode_responden))

timses$NOrgs <- rowSums(timses[,grep(names(timses), pattern="Q12_[0-9]*$")])


#################################################################################################
# Table 2 Data for CJ3 survey. Winning Broker Survey data available from Muhtadi 2018
#################################################################################################

# Think about the main candidate you worked for: before you became the korlap had you heard about or did you know the candidate you supported? 
tab <- matrix(table(timses[,"Q27"]))
tab <- cbind(tab, round(100*(tab/sum(tab)),2))
colnames(tab) <- c("Count","%")
rownames(tab) <- c("Knew him/her personally and was close to him/her","Knew him/her personally (had met) but was not especially close to him/her","Had heard of him/her but had never met him/her","Had never heard of him/her","Refused to Answer")
stargazer(tab, digits=2, out="[insert path here]", title="Before you became the korlap, had you heard about or did you know the candidate you supported?", label="tab:relationshipToCandidate")

#################################################################################################
# Table 3
#################################################################################################

# Do Brokers who knew the candidate talk more about his/her characteristics
summary(mChar <- polr(as.factor(Q26) ~ as.factor(Q27), data=timses[which(timses$Q26!=88 & timses$Q27!=88),], Hess=TRUE))
stargazer(mChar, type="html", out="[insert path here]",title="Does knowing the candidate affect how much you talk about him or her?", label="tab:talkAboutCand", covariate.labels=c("Never heard of candidate","Never met candidate","Knew, but not close to candidate"), notes="Excluded group is brokers who knew and were close to the candidate before working as a broker.")

#################################################################################################
# Table 4 Data for CJ3 survey. Winning Broker Survey data available from Muhtadi 2018
#################################################################################################

# What sort of people did you place on your list? (can be more than one category)
y <- grep(names(timses), pattern="^Q23_[0-9]")
y <- y[-grep(colnames(timses)[y], pattern="nama")]
tab <- as.matrix(sapply(timses[,y], table))
rownames(tab) <- c("Yes","No")
colnames(tab) <- c("Members of your family [ie household]","Relatives","Neighbours","Close friends","Acquaintances","Members of your professional group","Members of your religious community","Members of your community group")
# tab <- cbind(tab, round(100*(tab/sum(tab)),2))
rownames(tab) <- c("Members of your family [ie household]","Relatives","Neighbours","Close friends","Acquaintances","Members of your professional group","Members of your religious community","Members of your community group")
tab <- t(tab)
tab <- (tab[,1]/332)*100
stargazer(tab, digits=2, out="[insert path here]",title="What sort of people are on your list?", label="tab:whoOnYourList")

#################################################################################################
# Table 5 Data for CJ3 survey. Winning Broker Survey data available from Muhtadi 2018
#################################################################################################

#	When you were working as a korlap did you feel that your work was monitored by the kordes or other members of the candidate’s team?
tab <- as.matrix(table(timses$Q29))
tab <- cbind(tab, round(100*(tab/sum(tab)),2))
rownames(tab) <- c("Yes, often monitored","Yes, sometimes monitored","No, not monitored")
rownames(tab)[4] <- "Refuse to answer"
colnames(tab) <- c("Count","%")
stargazer(tab, digits=2, out="[insert path here]",title="When you were working as a korlap did you feel that your work was monitored by the kordes or other members of the candidate’s team?", label="tab:monitored")


#################################################################################################
# Voter Survey Data for Tables 6
#################################################################################################

pemilih <- read.dta(file="pemilih.dta")

  # ----- Cleaning ----- #
  # Repondent Code
  pemilih$voterID <- as.numeric(as.character(pemilih$no_kode_responden))
  
  pemilih$NOrgs <- rowSums(pemilih[,grep(names(pemilih), pattern="Q12_[0-9]")])

  # Replacement?
  table(pemilih$status_responden)
    pemilih$status_responden_en[pemilih$status_responden==1] <- "original"
    pemilih$status_responden_en[pemilih$status_responden==2] <- "replacement"
    # Why
    table(pemilih$alasan_lainnya)
    pemilih$alasan_lainnya <- gsub(x=as.character(pemilih$alasan_lainnya), pattern="^[[:space:]]*|[[:space:]]*$", replacement="")
    pemilih$alasan_lainnya_en <- NA
    pemilih$alasan_lainnya_en[pemilih$alasan_lainnya=="ALAMAT RESPONDEN FIKTIF"] <- "fake adress"
    pemilih$alasan_lainnya_en[grep(x=pemilih$alasan_lainnya, pattern="DATA FIKTIF|FIKTIF|NAMA FIKTIF|NAMA RESPONDEN ASLI FIKTIF")] <- "fake"
    pemilih$alasan_lainnya_en[pemilih$alasan_lainnya=="RESPONDEN TIDAK DITEMUKAN"]  <- "not found"
    pemilih$alasan_lainnya_en[pemilih$alasan_lainnya=="SALAH ALAMAT"] <- "miscarriage"
    pemilih$alasan_lainnya_en[pemilih$alasan_lainnya=="SUDAH DIWAWCARA TIMSES"] <- "already interviewed"
    pemilih$alasan_lainnya_en[pemilih$alasan_lainnya=="SUDAH PINDAH"] <- "moved"
    pemilih$alasan_lainnya_en[pemilih$alasan_lainnya=="TIDAK JELAS ALAMATNYA"] <- "unclear address"


    #################################################################################################
    # Table 6
    #################################################################################################    
    
  # Consequences of not voting after accepting money
  notVote <- pemilih[,grep(colnames(pemilih), pattern="Q48\\_")]
  notVote[notVote==2] <- 0
  notVote <- notVote[,c(1:7)]

  pdf(file="[insert path here]")
  par(mar=c(4.1, 10, 4.1, 2.1))
  barplot(colSums(notVote, na.rm=TRUE), horiz=TRUE, 
          names.arg = c("None", "Embarrassed", "No Help","No Vote Money Next Time",
                        "Return Vote Money","Verbal Abuse","Physical Violence"), 
          cex.names=.8, las=2, main="Consequences of not turning out\nafter accepting money from a candidate")
  dev.off()


  # Consequences of not voting for the candidate who gave you money
  notVoteForCand <- pemilih[,grep(colnames(pemilih), pattern="Q49\\_")]
  notVoteForCand[notVoteForCand==2] <- 0
  notVoteForCand <- notVoteForCand[,c(1:7)]


  pdf(file="[insert path here]")
  par(mar=c(4.1, 10, 4.1, 2.1))
  barplot(colSums(notVoteForCand, na.rm=TRUE), horiz=TRUE, names.arg = c("None", "Embarrassed", "No Help","No Vote Money Next Time","Return Vote Money","Verbal Abuse","Physical Violence"), cex.names=.8, las=2, main="Consequences of not voting\nfor the candidate who gave you money")
  dev.off()













  






